package cn.com.infcn.monitor.task.jvm;

import java.lang.management.CompilationMXBean;
import java.lang.management.ManagementFactory;

import org.apache.log4j.Logger;

import cn.com.infcn.monitor.bean.IFCJVMCompilation;
import cn.com.infcn.monitor.task.BaseTask;
import cn.com.infcn.monitor.util.IFCBeanUtils;

/**
 * 用于 Java 虚拟机的编译系统的管理接口。
 * 
 * @author jjs
 *
 */
public class JVMCompilationTask implements BaseTask {

	private static final Logger LOGGER = Logger.getLogger(JVMCompilationTask.class);

	@Override
	public IFCJVMCompilation execute() throws Exception {
		return jvmCompilation();
	}

	public IFCJVMCompilation jvmCompilation() {

		try {
			CompilationMXBean compilationMXBean = ManagementFactory.getCompilationMXBean(); 

			IFCJVMCompilation jvmCompilation = new IFCJVMCompilation();
			
			IFCBeanUtils.copyProperties(compilationMXBean, CompilationMXBean.class, jvmCompilation);

			return jvmCompilation;
		} catch (Exception e) {
			LOGGER.warn(e.getMessage(), e);
		}

		return null;
	}
	
	/*private void copyProperties(CompilationMXBean compilationMXBean, IFCJVMCompilation jvmCompilation){
		jvmCompilation.setIsCompilationTimeMonitoringSupported(compilationMXBean.isCompilationTimeMonitoringSupported());
		jvmCompilation.setName(compilationMXBean.getName());
		jvmCompilation.setTotalCompilationTime(compilationMXBean.getTotalCompilationTime());
	}*/

	public static void main(String[] args) {
		JVMCompilationTask task = new JVMCompilationTask();
		IFCJVMCompilation ifcCompilation = task.jvmCompilation();

		System.out.println(ifcCompilation.getTotalCompilationTime());
		System.out.println(ifcCompilation.getIsCompilationTimeMonitoringSupported());
		System.out.println(ifcCompilation.getName());
	}
}
